<?php
/**
 * ViewMySchedule function
 *
 * Return schedule for specified employee
 *
 * @author Will Zhao <kexinzhao666@gmail.com>
 */
include_once 'UTDBGlobal.php';
include_once 'UTerrorcode.php';
include_once 'UTcheckAuth.php';
/**
 * @param $userName string
 * @param $userPassHash string
 * @return array|null string
 */
function viewMySchedule($userName, $userPassHash)
{
    global $dbaddress;
    global $dbuser;
    global $dbpassword;
    global $dbdatabasename;

	$array=array();
	if(checkAuthentication($userName,$userPassHash))
	{
		$conn = mysqli_connect($dbaddress,$dbuser,$dbpassword,$dbdatabasename);
                $sql="select roles.roleName,
roledate.dateID,roledate.roleID,roledate.startTime,
roledate.endTime,roledate.shiftStatus,dates.datestamp 
from  employees left join (dates,roledate,account,roles) 
on roledate.empID=employees.empID and
dates.dateID=roledate.dateID
and roledate.empID=account.empID
and roles.roleID=roledate.roleID
where account.accName='".$userName."'";
		$result=$conn->query($sql);
		if ($result)
		{
			 if($result->num_rows>0)
			 {
			    		while($row=$result->fetch_array())
						{
						$array[] = array(
								'roleName'=>$row['roleName'],
								'roleID'=>$row['roleID'],
								'dateID'=>$row['dateID'],
								'date'=>$row['datestamp'],
								'startTime'=>$row['startTime'],
								'endTime'=>$row['endTime'],
								'shiftStatus'=>$row['shiftStatus']);
						}
						$error=ErrorCode::successRead;
			 }  
		}
		else
		{
		      $error =ErrorCode::failRead;
		}
	}
	else
	{
		//echo 'unknown user';
		$error =ErrorCode::authFailRead;
	}
        array_unshift($array,$error);
				$result->free();
				$conn->close();
        return $array;
}
?>
